﻿<?php
global $m, $tab, $a;
require_once($AppUI->getModuleClass('projects'));
require_once($AppUI->getMasterDataClass('mdtype'));
require_once($AppUI->getMasterDataClass('mdstatus'));
require_once($AppUI->getModuleClass('companies'));

$perm = &$AppUI->acl();
if (!$perm->userHasRole($AppUI->user_id, 'Accounting')) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect("m=home&tab=0");
	exit;
}
$customer_id = intval( dPgetParam( $_POST, 'project_company', 0 ) );
$status_id = intval( dPgetParam( $_POST, 'project_rstatus', 0 ) );
$keyword = dPgetParam( $_POST, 'keyword', '' );

$from_date = dPgetParam( $_POST, 'complete_from_date', '' );

if ($from_date != '') {
	$from_date = new CDate($from_date);
}

$to_date = dPgetParam( $_POST, 'complete_to_date', '' );

if ($to_date != '') {
	$to_date = new CDate($to_date);
}

$customer = new CCompany();
$customer->load($customer_id);
$project_code = dPgetParam( $_POST, 'project_code', '' );

require_once($AppUI->getMasterDataClass('mdcurrency'));
$objMD = new CMdCurrency();
$curArr = $objMD->getCurrencyList();

$pro = new CProject();
$totalRows = 0;
$projects = $pro->getList($_POST, $totalRows);

// trangdt add
include_once(DP_BASE_DIR."/modules/reports/xtpl/xtemplate.class.php");
$xtpl = new XTemplate(DP_BASE_DIR."/modules/reports/AccountReceivable.xml");
$size = count($projects);

// begin currency row
foreach($curArr as $key=>$cur){
	if ($cur == 'VND') {
		continue;
	}
	$xtpl->assign('cur_name',$cur);
	$xtpl->assign('ex_rate',dPgetParam( $_POST, $key . "_vnd", 1 ));
	$xtpl->parse('main.currow');
}

// end currency row

//begin rows
foreach ($projects as $row)
{
	$currency = $row['project_currency'];
	$exchange = dPgetParam( $_POST, $currency . "_vnd", 1 );
	$inter = $pro->getExpenseAmount($row['project_id'], 1);
	$onbehalf = $pro->getExpenseAmount($row['project_id'], 2);
	$other = $pro->getExpenseAmount($row['project_id'], 3);
	$sa = $row['sa_sales_amount'];
	$vat = $pro->getVATAmount($row['project_id']);
	$ar = $sa + $vat;

	$sumInter += $inter;
	$sumOnBehalf += $onbehalf;
	$sumOther += $other;
	$sumSA += $exchange * $sa;
	$sumVAT += $exchange * $vat;
	$sumAR += $exchange * $ar;

	$xtpl->assign('DATA',$row);
	$xtpl->assign('currency',$curArr[$row['project_currency']]);

	$xtpl->assign('sales_amount',$row['sa_sales_amount']);
	$xtpl->assign('vat_amount',$vat);
	$xtpl->assign('acount_receivable',$ar);

	$xtpl->assign('inter_ex', $inter);
	$xtpl->assign('onbehalft_ex', $onbehalf);
	$xtpl->assign('other_ex', $other);

	$xtpl->assign('sales_amount1', $exchange *$row['sa_sales_amount']);
	$xtpl->assign('vat_amount1', $exchange * $vat);
	$xtpl->assign('acount_receivable1', $exchange * $ar);
	$xtpl->parse('main.row');
}
//end rows
//main


//$from_date = "from date";
//$to_date = "to date";
if($customer != null){
	$customer_code =$customer->company_code;
	$customer_name =$customer->company_name;
}


/*
$from_date = $from_date ? $from_date->format( "%D" ):'';
$to_date = $to_date ? $to_date->format( "%D"):'';
*/
$from_date = $from_date ? $from_date->format( "%Y-%m-%dT%T.000" ):'';
$to_date = $to_date ? $to_date->format( "%Y-%m-%dT%T.000"):'';

$xtpl->assign('FROM_DATE', replace($from_date));
$xtpl->assign('TO_DATE', replace($to_date));
//$xtpl->assign('CUSTOMER_CODE', $customer_code);
//$xtpl->assign('CUSTOMER_NAME', $customer_name);
//$xtpl->assign('REFERENCE_CODE', $project_code);

$xtpl->assign('SIZE', $size+20+count($curArr));
$xtpl->assign('RESULT', $size);
$xtpl->parse('main');
$out = $xtpl->out('main');

function replace($date){
	if($date != null){
		return 	'<Data ss:Type="DateTime">'.$date.'</Data>';
	}
	else{
		return $date;
	}
}

$temp_dir = dPgetConfig( 'root_dir' )."/files/temp";
if ($fp = fopen( "$temp_dir/temp$AppUI->user_id.xls", 'wb' )) {
	fwrite( $fp, $out);
	fclose( $fp );
	header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
	header("Content-Disposition: inline; filename=\"AccountReceivable.xls\"");
	readfile("$temp_dir/temp$AppUI->user_id.xls");
}
// trangdt end
?>